package sse.ustc.cc.fragment;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import sse.ustc.cc.activity.CourseDetailAty;
import sse.ustc.cc.activity.R;
import sse.ustc.cc.activity.CourseAty;
import sse.ustc.cc.custom.CustomDayScheduleView;
import sse.ustc.cc.custom.CustomDayScheduleView.OnItemClassClickListener;
import sse.ustc.cc.custom.CustomDayScheduleView.onItemNoneClassClickListener;
import sse.ustc.cc.db.DBService;
import sse.ustc.cc.util.ClassInfo;
import sse.ustc.cc.util.DataString;
import sse.ustc.cc.util.MyLinearLayout;
import sse.ustc.cc.util.ScaleGestureListener;
import sse.ustc.cc.util.SettingInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class DayClassInfoFrag extends Fragment {
	protected static final Fragment AddCourseFragment = null;
	private int weekNum;
	private static boolean first = true;
	private Context mContext;
	boolean longpush = false;
	private final static String TAG = "day_class";
	private CustomDayScheduleView scheduleView;
	private ArrayList<ClassInfo> classList;// 获取课表信息

	// 在课程详细中获取课程信息的时候用
	public ArrayList<ClassInfo> getClassList() {
		return classList;
	}

	public void setClassList(ArrayList<ClassInfo> classList) {
		this.classList = classList;
	}

	private ArrayList<SettingInfo> settingList, dyweekList, dyDayList;// 获取设置和当前周数信息
	private ArrayList<String> courseList;// 获取课程信息
	private ArrayList<String> setList;// 获取设置信息

	public static DayClassInfoFrag newInstance(int num) {
		DayClassInfoFrag f = new DayClassInfoFrag();
		Bundle args = new Bundle();
		args.putInt("num", num);
		f.setArguments(args);

		return f;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		mContext = activity;

		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		weekNum = getArguments() != null ? getArguments().getInt("num") : 1;

		LayoutInflater.from(mContext);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreateView(inflater, container, savedInstanceState);
		MyLinearLayout myLinearLayout = new MyLinearLayout(mContext, null);
		RelativeLayout myRelativeLayout = (RelativeLayout) inflater.inflate(
				R.layout.view_day_classinfo, container, false);
		myLinearLayout.addView(myRelativeLayout);

		scheduleView = (CustomDayScheduleView) myRelativeLayout
				.findViewById(R.id.dayscheduleView);

		// 获取今天是周几
		DataString dataString = new DataString();
		Log.e(TAG, dataString.StringData().toString());

		int day = Integer.parseInt(dataString.StringData().toString());

		// 滑动时星期数会增加
		day += weekNum;
		/* 初始化 */
		courseList = new ArrayList<String>();// 课程列表声明
		setList = new ArrayList<String>();// 设置列表声明
		dyweekList = new ArrayList<SettingInfo>();// 动态周数声明
		dyDayList = new ArrayList<SettingInfo>();// 动态周几声明
		dyweekList.clear();// 清空
		dyDayList.clear();// 清空
		DBService ds = new DBService(mContext);// 数据库声明
		courseList = ds.selectCourse();// 选择课表信息，存放在list里面
		setList = ds.selectSetting();// 选择设置信息，存放在list里面
		/* 只在第一次刷新onCreateView 时载入 */

		/* 判断课程表是否存在课程 */
		if (courseList != null && courseList.size() > 0) {// 存在课程
			Log.e("rirsttime", first + "");

			// 获取设置表里面的当前周数
			int week = Integer.parseInt(setList.get(0));
			/* 将星期数循环显示 */
			if (day >= 7) {
				int yuday = day / 7 ;
				day = day % 7;
				if (day == 0) {
					day = 7;
				}
				week = week + yuday;
			}
			String lookDay = day + "";// 周几的选择
			SettingInfo settingDayInfo = new SettingInfo();
			settingDayInfo.setDyday(lookDay);
			dyDayList.add(settingDayInfo);// 将星期数显示到界面顶端
			SettingInfo settingInfo = new SettingInfo();
			settingInfo.setDyweek(week + "");
			dyweekList.add(settingInfo);// 将周数显示到界面的左上角
			getClassData(lookDay,week);// 根据周几选择界面显示的课程
			getSettingData();// 选择设置信息表的内容
			scheduleView.setClassList(classList);// 将课程信息导入到课表View中
			scheduleView.setSettingList(settingList);// 将设置信息导入到课表View中
			scheduleView.setDyweekList(dyweekList);// 将动态周数导入到View中
			scheduleView.setDyDayList(dyDayList);// 将动态的周几导入到View中
			/* 长按效果的监听 */
			scheduleView.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {

					Toast.makeText(mContext, "I got a long press!",
							Toast.LENGTH_SHORT).show();
					longpush = true;
					return false;
				}
			});
			/* 点击课程效果的监听 */
			scheduleView
					.setOnItemClassClickListener(new OnItemClassClickListener() {

						@Override
						public void onClick(ClassInfo classInfo) {
							if (!longpush) {
								Toast.makeText(mContext,
										"您点击的课程是：" + classInfo.getClassname(),
										Toast.LENGTH_SHORT).show();
								// 实例化 Bundle，设置需要传递的参数
								Intent intent = new Intent(getActivity(),
										CourseDetailAty.class);
								ArrayList<ClassInfo> courseInfoList = getCourseInfo(classInfo
										.getClassname());
								intent.putExtra("courseinfo", courseInfoList);
								startActivity(intent);
								longpush = false;
							}
							longpush = false;
						}

						/**
						 * 获得与点击课程名称相同的课程信息
						 * 
						 * @param courseName
						 * @return
						 */
						private ArrayList<ClassInfo> getCourseInfo(
								String courseName) {
							ArrayList<ClassInfo> courseInfoList = new ArrayList<ClassInfo>();
							for (int i = 0; i < classList.size(); i++) {
								if (courseName.equals(classList.get(i)
										.getClassname())) {
									courseInfoList.add(classList.get(i));
								}
							}
							return courseInfoList;
						}
					});
			/* 点击其他区域效果的监听 */
			scheduleView
					.setonItemNoneClassClickListener(new onItemNoneClassClickListener() {

						@Override
						public void onClick() {
							// TODO Auto-generated method stub

							// addCourseDdialog();
						}
					});

		} else {// 没有课程信息，提示增加课程的对话框
			Log.e("nocourse", "nocourse");
			if (first) {
				first = false;

				addCourseDdialog();
			} else {
				//first = false;
				// 第二次刷新oncreatview
			}
		}

		// 添加手势缩放
		ScaleGestureListener sg = new ScaleGestureListener(myLinearLayout,
				mContext, TAG);
		final ScaleGestureDetector mScaleGestureDetector = new ScaleGestureDetector(
				getActivity(), sg);
		myLinearLayout.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return mScaleGestureDetector.onTouchEvent(event);
			}
		});
		return myLinearLayout;
	}

	/**
	 * 提示增加课程
	 */
	protected void addCourseDdialog() {
		AlertDialog.Builder builder = new Builder(getActivity());
		builder.setMessage("添加课程否？");
		builder.setTitle("提示");
		builder.setPositiveButton("是", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				/* 跳转到课程增加界面 */
				Intent intent = new Intent();
				intent.setClass(mContext, CourseAty.class);
				Bundle bundle = new Bundle();
				bundle.putString("addcourse", "add");
				intent.putExtras(bundle);
				startActivity(intent);

			}
		});
		builder.setNegativeButton("否", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	/**
	 * 提获取设置信息表内容
	 */
	private void getSettingData() {
		settingList = new ArrayList<SettingInfo>();
		SettingInfo settingInfo = new SettingInfo();
		settingInfo.setCurrentWeek(setList.get(0));
		settingInfo.setMaxSection(setList.get(1));
		settingInfo.setSectionTime(setList.get(2));
		settingList.add(settingInfo);

	}

	/**
	 * 根据今天是星期几,所在的周数来提获取课程信息表内容
	 * 
	 * @param 参数为星期几，周数
	 */
	private void getClassData(String day,int week) {
		classList = new ArrayList<ClassInfo>();
		int j = courseList.size() / 13;// 确定有几门课程
		for (int i = 0; i < j; i++) {
			/* 显示当前周数有课的课程信息 */
			if (Integer.parseInt(courseList.get(13 * i + 1)) == Integer
					.parseInt(day)&&Integer.parseInt(courseList.get(13 * i + 10)) >= week
							&& week >= Integer
									.parseInt(courseList.get(13 * i + 3))) {
				ClassInfo calssInfo = new ClassInfo();
				calssInfo.setClassname(courseList.get(13 * i));// 获取课程名称
				calssInfo.setClassRoom(courseList.get(13 * i + 8));// 获取课程的教室
				calssInfo.setTeacher_name(courseList.get(13 * i + 4));

				calssInfo.setWeek_begin(courseList.get(13 * i + 3));// 获取周数结束时间
				calssInfo.setWeek_end(courseList.get(13 * i + 10));// 获取周数结束时间

				calssInfo.setFromClassNum(Integer.parseInt(courseList
						.get(13 * i + 2)));// 获取课程 节数的开始
				calssInfo.setWeekday(Integer.parseInt(courseList
						.get(13 * i + 1)));// 获取周几
				calssInfo.setSection_end(courseList.get(13 * i + 9));// 获取课程
																		// 节数的结束
				calssInfo.setSection_begin(courseList.get(13 * i + 2));// 获取课程
																		// 节数的结束

				calssInfo.setClassNumLen(Integer.parseInt(courseList
						.get(13 * i + 9))
						- Integer.parseInt(courseList.get(13 * i + 2)) + 1);// 课程节数的间隔
				calssInfo.setCourse_begin_time(courseList.get(13 * i + 11));// 获取周数结束时间
				calssInfo.setCourse_end_time(courseList.get(13 * i + 12));// 获取周数结束时间
				Log.i(TAG,
						">>>>>>>>>>>>>>>getWeekday" + calssInfo.getWeek_begin());
				classList.add(calssInfo);
			}

		}

	}
}
